package role

import (
	"fmt"
	"gin_demo/internal/consts"
	"gin_demo/internal/model"
	"gin_demo/utility"
	"time"

	"github.com/gin-gonic/gin"
)

func List(ctx *gin.Context, req PageList) {
	list := []model.Role{}
	var count int64
	tx := consts.App.DB.Limit(req.Limit).Offset(req.Page - 1).Find(&list).Count(&count)
	if tx.Error != nil {
		consts.App.Log.Error(tx.Error.Error())
		utility.Fail(ctx, "角色加载失败")
	} else {
		res := RoleRes{
			Count: count,
			List:  list,
		}
		utility.Success(ctx, res)
	}
}

func Create(ctx *gin.Context, req AddRoleReq) {
	list := model.Role{
		Name: req.Name,
		Desc: req.Desc,
	}
	tx := consts.App.DB.Create(&list)
	if tx.Error != nil {
		consts.App.Log.Error(tx.Error.Error())
		utility.Fail(ctx, "角色添加失败")
	} else {
		utility.Success(ctx, list)
	}
}

func Update(ctx *gin.Context, req UpdateRoleReq) {
	list := model.Role{
		Name: req.Name,
		Desc: req.Desc,
	}
	tx := consts.App.DB.Model(&list).Where("id=?", req.Id).Updates(&list)
	if tx.Error != nil {
		consts.App.Log.Error(tx.Error.Error())
		utility.Fail(ctx, "角色更新失败")
	} else {

		utility.Success(ctx, list)
	}
}

func Delete(ctx *gin.Context, req SoftDeleteReq) {
	list := model.Role{}
	tx := consts.App.DB.Model(&list).Where("id=?", req.Id).Update("deleted_at", time.Now())
	if tx.Error != nil {
		consts.App.Log.Error(tx.Error.Error())
		utility.Fail(ctx, "角色删除失败")
	} else {

		utility.Success(ctx, "删除成功")
	}
}

func CreadRolePermission(ctx *gin.Context, req RoleAddPermission) {
	list := model.RolePermissions{
		RoleID:       req.RoleId,
		PermissionID: req.PermissionID,
	}
	tx := consts.App.DB.Create(&list)
	if tx.Error != nil {
		fmt.Println(tx.Error)
		consts.App.Log.Error(tx.Error.Error())
	} else {
		utility.Success(ctx, list)
	}
}

func DeleteRolePermission(ctx *gin.Context, req RoleSoftDeletePermission) {
	list := model.RolePermissions{}
	tx := consts.App.DB.Model(&list).Where("role_id=? and permission_id=?", req.RoleId, req.PermissionID).Update("deleted_at", time.Now())
	if tx.Error != nil || tx.RowsAffected == 0 {
		utility.Fail(ctx, "角色权限删除失败")
	} else {

		utility.Success(ctx, "角色权限删除成功")
	}
}

func RolePermission(ctx *gin.Context, req PageList) {
	list := []model.RolePermissions{}
	var count int64
	tx := consts.App.DB.Limit(req.Limit).Offset(req.Page - 1).Find(&list).Count(&count)
	if tx.Error != nil {
		consts.App.Log.Error(tx.Error.Error())
		utility.Fail(ctx, "角色权限加载失败")
	} else {
		// 根据查询出来的id，查找对应的数据内容
		role := []model.Role{}
		permission := []model.Permission{}
		l := RolePermissions{}
		res := RolePermissionRes{}
		for i := 0; i < len(list); i++ {
			consts.App.DB.Where("id=?", list[i].RoleID).Find(&role)
			consts.App.DB.Where("id=?", list[i].PermissionID).Find(&permission)
			for _, v := range role {
				for _, t := range permission {
					l.Id = v.ID
					l.Role = v.Name
					l.Permission = t.Name
					res.List = append(res.List, l)
				}
			}
		}
		utility.Success(ctx, res)
	}
}

func UpdateRolePermission(ctx *gin.Context, req RoleUpdatePermission) {
	list := model.RolePermissions{
		RoleID:       req.RoleId,
		PermissionID: req.PermissionID,
	}
	tx := consts.App.DB.Model(&list).Where("id=?", req.Id).Updates(&list)
	if tx.Error != nil || tx.RowsAffected == 0 {
		utility.Fail(ctx, "角色更新失败")
	} else {

		utility.Success(ctx, list)
	}

}
